package com.ostrich.cloud.ostrichUserService.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.additional.update.impl.UpdateChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ostrich.cloud.ostrichUserService.common.stateEnum.CusLocationEnum;
import com.ostrich.cloud.ostrichUserService.infra.mapper.CustomerLocationMapper;
import com.ostrich.cloud.ostrichUserService.infra.model.CustomerLocation;
import com.ostrich.cloud.ostrichUserService.service.CustomerLocationService;
import com.ostrich.ostrichUserService.param.location.CusLocationParam;
import org.redisson.api.RGeo;
import org.redisson.api.RScoredSortedSet;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 用户地址表 服务实现类
 * </p>
 *
 * @author Lucas
 * @since 2022-10-26
 */
@Service
public class CustomerLocationServiceImpl extends ServiceImpl<CustomerLocationMapper, CustomerLocation> implements CustomerLocationService {

    @Resource
    private CustomerLocationMapper customerLocationMapper;

    @Resource
    private RedissonClient redissonClient;

    private static  String KEY = "harvest";

    @Override
    public void addLocation(CusLocationParam cusLocationParam) {

        CustomerLocation customerLocation = BeanUtil.copyProperties(cusLocationParam, CustomerLocation.class);
        customerLocation.setStatus(CusLocationEnum.NOT_USER.getCode());
        int id = customerLocationMapper.insert(customerLocation);
    }

    /**
     * 删除收获地址在redis和数据库
     * @param locationId
     */
    @Override
    public void removeCusLocation(Integer locationId) {
        CustomerLocation customerLocation = customerLocationMapper.selectById(locationId);
        if (CusLocationEnum.USE.getCode().equals(customerLocation.getStatus())){
            RScoredSortedSet<Object> scoredSortedSet = redissonClient.getScoredSortedSet(KEY);
            scoredSortedSet.remove(locationId);
        }
        customerLocationMapper.deleteById(locationId);
    }

    /**
     * 修改默认地址
     * @param locationId 地址id
     * @param status 修改状态
     */
    @Override
    @Transactional
    public void putCusLocationState(Integer locationId, String status,Integer parentId) {
        RScoredSortedSet<Object> scoredSortedSet = redissonClient.getScoredSortedSet(KEY);
        if (CusLocationEnum.USE.getCode().equals(status)) {
            List<CustomerLocation> list = customerLocationMapper.selectList(new QueryWrapper<CustomerLocation>()
                    .eq("parent_id", parentId));
            for (CustomerLocation s : list) {
                if (CusLocationEnum.USE.getCode().equals(s.getStatus())) {
                    customerLocationMapper.update(null, new UpdateWrapper<CustomerLocation>()
                            .eq("customer_location_id", s.getCustomerLocationId())
                            .set("status", CusLocationEnum.NOT_USER.getCode()));
                    scoredSortedSet.remove(s.getCustomerLocationId());
                }
            }
            /**
             * 添加地址到redis
             */
            CustomerLocation cus = customerLocationMapper.selectById(locationId);

            RGeo<Object> geo = redissonClient.getGeo(KEY);
            geo.add(Double.parseDouble(cus.getCustomerLatitude()),Double.parseDouble(cus.getCustomerLogitude()),
                     cus.getCustomerLocationId());

            customerLocationMapper.update(null, new UpdateWrapper<CustomerLocation>()
                    .eq("customer_location_id", locationId)
                    .set("status", status));
        }else {
            scoredSortedSet.remove(locationId);
            customerLocationMapper.update(null, new UpdateWrapper<CustomerLocation>()
                    .eq("customer_location_id", locationId)
                    .set("status", status));
        }
    }
}
